*** Replication Code for The Effect of the Affordable Care Act and Racial Dynamics on Federal Medicaid Transfers***
clear all

***** Changing color setting to Color Blind******

local install_switch = 0
if `install_switch'  {
ssc install reghdfe
ssc install estout
ssc install ftools
ssc install lassopack
ssc install gtools
ssc install maptile
ssc install spmap
}
reghdfe, compile

ssc install blindschemes, replace all
set scheme plotplain, permanently

***************************************
*****Setting globals*******************
***************************************

global root ""
global ready	"${root}/Data_Development/Ready"
global plotdir 	"${root}/Figures_Tables_Output"     // paper figure directory


************* Open dataset *******

use "$ready/dataset_finalMay2022.dta", clear 

*** Generating log of total population to add it as control variable ***

gen log_pop= ln(totalpop)

label var log_pop "Total Pop (ln)"

/// Generating waiver variable ///

gen waiver=0
replace waiver=1 if state_po=="AR" & year>=2018
replace waiver=1 if state_po=="AZ" & year>=2018
replace waiver=1 if state_po=="KY" & year>=2018
replace waiver=1 if state_po=="MI" & year>=2018
replace waiver=1 if state_po=="NH" & year>=2018
replace waiver=1 if state_po=="OH" & year>=2018
replace waiver=1 if state_po=="IN" & year>=2018
replace waiver=1 if state_po=="UT" & year>=2018
replace waiver=1 if state_po=="SC" & year>=2018
replace waiver=1 if state_po=="WI" & year>=2018

gen aca_waiver=acaflag*waiver
gen eligible_waiver=share_eligible*waiver

//Label variables//
label var medicaidtransfers "Medicaid Transfers"
label var totalpop "Total Pop"
label var totalpop_black "Total Black Pop"
label var AFDCTANFBenefitfor3personf "TANF"
label var FSSNAPBenefitfor3personfam "SNAP"
label var RefundableStateEITC1Yes "EITC Refundable"
label var StateEITCRate "EITC Rate"
label var StateMinimumWage "State Minimum Wage"
label var Unemploymentrate "Unemployment Rate"
label var democrat_county "Democrat County"
label var waiver "Waiver State"

*** Generating partisan alignment County - State (a county where the democrat presidencial candidate won and the governor is a democrat)

gen partisan_alignment=0
replace partisan_alignment=1 if democrat_county==1 & GovernorisDemocrat1Yes=="1"


*** Turning missing values for the share_eligible equal to zero before and remain the same after ***
//replace share_eligible=0 if year<2010
//bysort fipscode (year): replace share_eligible= share_eligible[_n-1] if year>2010

bysort fipscode: egen share_eligible_c=max(share_eligible)

*** Centering Variables ***

local vlist share_black 

foreach v of varlist `vlist'{
egen `v'_mean=mean(`v')
replace `v'_mean=0 if `v'_mean==.
}


foreach v of varlist `vlist'{
gen `v'_c=`v'-`v'_mean
}

**** Generate interaction term between ACA and Proportion of Black population

gen aca_black = acaflag*share_black_c
label var aca_black "ACA*Black"

*** Generate interaction term between ACA and Proportion of Eligible Pop

gen aca_eligible = acaflag*share_eligible_c
label var aca_eligible "ACA*Eligible"


***Generating squared var***
local vlist share_black share_eligible
foreach v of varlist `vlist'{
gen `v'_sq=`v'^2
}
***Generating quartiles****
local vlist share_black share_eligible
foreach v of varlist `vlist'{
xtile `v'_q= `v', nq(4)
tab `v'_q, gen(`v'_q)
} 

***Generating interaction term between ACA and squared and quartiles****

local vlist share_black_sq share_eligible_sq share_black_q1 share_black_q2 share_black_q3 share_black_q4

foreach v of varlist `vlist'{
gen int_`v'=`v'*acaflag
label var int_`v' "ACA*`v'"
}

*** Generating interaction term between ACA and control variables

local vlist AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate StateMinimumWage Unemploymentrate 
foreach v of varlist `vlist'{

gen int_`v'=acaflag*`v'
replace int_`v'=0 if int_`v'==.
label var int_`v' "ACA*`v'"
}

*** Generating interaction term between ACA, proportion of black pop and partisan_alignment
gen aca_democrat=acaflag*partisan_alignment
gen aca_democrat_c=acaflag*democrat_county
gen aca_black_dem=aca_black*partisan_alignment 
gen aca_black_dem_c=aca_black*democrat_county


***Generating interaction term between ACA, squared and quartiles, and partisan alignment****

local vlist int_share_black_sq int_share_black_q1 int_share_black_q2 int_share_black_q3 int_share_black_q4

foreach v of varlist `vlist'{
gen `v'_demo=`v'*partisan_alignment
label var `v'_demo "ACA*`v'*Democrat"
}

foreach v of varlist `vlist'{
gen `v'_dc=`v'*democrat_county
label var `v'_dc "ACA*`v'*County_Democrat"
}

****************************		
***Descriptive Statistics***
****************************

//First I generate an indicator variable for treatment status//

gen expanded=0
replace expanded=1 if acaflag!= 0

//Label variables//
label var medicaidtransfers "Medicaid Transfers"
label var totalpop "Total Pop"
label var totalpop_black "Total Black Pop"
label var AFDCTANFBenefitfor3personf "TANF"
label var FSSNAPBenefitfor3personfam "SNAP"
label var RefundableStateEITC1Yes "EITC Refundable"
label var StateEITCRate "EITC Rate"
label var StateMinimumWage "State Minimum Wage"
label var Unemploymentrate "Unemployment Rate"
label var democrat_county "Democrat County"
label var waiver "Waiver State"


global covariates "medicaidtransfers medicaidtransfers_pc totalpop totalpop_black  share_black AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate StateMinimumWage Unemploymentrate democrat_county waiver"
estpost sum $covariates if expanded==0
esttab using "$plotdir/Descriptive_NonExpansion.csv", cells("mean sd count") noobs replace
	
global covariates "medicaidtransfers medicaidtransfers_pc totalpop totalpop_black  share_black AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate StateMinimumWage Unemploymentrate democrat_county waiver"
estpost sum $covariates if expanded==1
esttab using "$plotdir/Descriptive_Expansion.csv", cells("mean sd count") noobs replace

**************************
*****Main Analysis********
**************************


** Setting dataset as a panel **

xtset fipscode year 


***Getting baseline mean**
summ medicaidtransfers_pc if year<2014
local mean : di %3.2f r(mean) 

***Running DiD***

xtreg medicaidtransfers_pc i.year log_pop aca_eligible AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate  Unemploymentrate democrat_county waiver int_Unemploymentrate  int_StateEITCRate int_RefundableStateEITC1Yes int_FSSNAPBenefitfor3personfam int_AFDCTANFBenefitfor3personf aca_democrat_c aca_waiver acaflag share_black  aca_black, fe vce(robust)
	estadd scalar base=`mean'
	estimates store cmedicaidtransfers_pc
		
xtreg medicaidtransfers_pc i.year log_pop aca_eligible AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate  Unemploymentrate democrat_county waiver int_Unemploymentrate  int_StateEITCRate int_RefundableStateEITC1Yes int_FSSNAPBenefitfor3personfam int_AFDCTANFBenefitfor3personf aca_democrat_c aca_waiver acaflag share_black  aca_black int_share_black_sq, fe vce(robust)
	estimates store cmedicaidtransfers_pc_sq
	
xtreg medicaidtransfers_pc i.year AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate  Unemploymentrate democrat_county waiver log_pop int_Unemploymentrate  int_StateEITCRate int_RefundableStateEITC1Yes int_FSSNAPBenefitfor3personfam int_AFDCTANFBenefitfor3personf aca_democrat aca_waiver acaflag aca_democrat_c share_black aca_eligible int_share_black_q2 int_share_black_q3 int_share_black_q4 , fe vce(robust)
	estimates store cmedicaidtransfers_pc_q
	
	esttab  cmedicaidtransfers_pc cmedicaidtransfers_pc_sq cmedicaidtransfers_pc_q ///
		using "$plotdir/TableDiD_All_Models_waiver.csv", ///
		b(a3) se(3)  nogaps  star(* 0.10 ** 0.05 *** 0.01) ///
		label title("DID Coefficients: Medicaid Transfers, ACA and Racial Demographics") ///
		mtitles("Linear Model" "Quadratic Polynomial Model" "Quantile Model" )  ///
		rename(`rename_opt') coeflabels(`coeflabels_opt') stats(N base, ///
		labels(`"Observations"' `"Baseline DV mean"')) replace	

*** Plotting Predicted Values of Medicaid Transfers ***
xtreg medicaidtransfers_pc i.year log_pop aca_eligible AFDCTANFBenefitfor3personf FSSNAPBenefitfor3personfam RefundableStateEITC1Yes StateEITCRate  Unemploymentrate democrat_county waiver int_Unemploymentrate  int_StateEITCRate int_RefundableStateEITC1Yes int_FSSNAPBenefitfor3personfam int_AFDCTANFBenefitfor3personf aca_democrat_c aca_waiver acaflag share_black  aca_black int_share_black_sq, fe vce(robust)

predict y_bar, xb
bysort fipscode year : egen mean_y_bar= mean(y_bar)
sort share_black
	# delimit ;
     quietly twoway
		(scatter mean_y_bar share_black,
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small)),
        xline(-.5 , lwidth(2.2) lcolor(gs10)) 
        xtitle("Black Population Share") ytitle("Estimated Medicaid Transfers Per Capita", size(vsmall))
		graphregion(color(white))	
        legend(off)
        graphregion(margin(r+5))
     ;
	 
    # delimit cr
drop y_bar
drop mean_y_bar	
			

********************************
*******Event Study Test*********
********************************


gen ysa=year-aca
replace ysa=9999 if ysa==.

tab ysa, gen(ysa_)

*Make interaction terms between event time dummies and black
foreach i in ysa_6  ysa_7 ysa_8 ysa_9 ysa_10 ysa_11 ysa_12 ysa_13 ysa_14 ysa_15 ysa_16 ysa_17 ysa_18 ysa_19 ysa_20 ysa_21 ysa_22 ysa_23  ysa_24 ysa_25{
	generate `i'_black=`i'*c.share_black
}


***Running regression
reghdfe medicaidtransfers_pc ysa_6  ysa_7 ysa_8 ysa_9 ysa_10 ysa_11 ysa_12 ysa_13 ysa_14 ysa_15 ysa_16 ysa_17 ///
		ysa_18 ysa_19 ysa_21 ysa_22 ysa_23  ysa_24 ysa_25 ///
		ysa_6_black ysa_7_black ysa_8_black ysa_9_black ysa_10_black ysa_11_black ysa_12_black ysa_13_black ///
		ysa_14_black ysa_15_black ysa_16_black ysa_17_black ysa_18_black ysa_19_black ysa_21_black ///
		ysa_22_black ysa_23_black ysa_24_black ysa_25_black, absorb(fipscode year) vce(cluster fipscode)
		
****Generating estimates to plot **** loop from period 6 to 25 but skip period 20
matrix define es = J(2,20,.)
local black = .09
forvalues j = 6(1)25{
	if `j' < 20  {
		local c = `j' - 5
		matrix es[1,`c']= _b[ysa_`j'] + `black' * _b[ysa_`j'_black]
		lincom _b[ysa_`j'] + `black' * _b[ysa_`j'_black]
		matrix es[2,`c']=r(se)
							}
	else if `j'>20 {
		local c = `j'- 5
		matrix es[1,`c']= _b[ysa_`j'] + `black' * _b[ysa_`j'_black]
		lincom _b[ysa_`j'] + `black' * _b[ysa_`j'_black]
		matrix es[2,`c']=r(se)
		}
	else if `j' == 20 {
		*do nothing
	}
	
} 

matrix colnames es = -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5
matrix list es

coefplot matrix(es[1]), se(es[2]) mlcolor(gray) lwidth(vthin)  lcolor(black) recast(connected) ciopts(recast(rcap)) drop(_cons ysa_6  ysa_7 ysa_8 ysa_9 ysa_10 ysa_11 ysa_12 ysa_13 ysa_14 ysa_15 ysa_16 ysa_17 ysa_18 ysa_19 ysa_21 ysa_22 ysa_23  ysa_24 ysa_25) xline(15) vertical omitted baselevels title("County Level Events" "Medicaid Transfers P/C") ytitle(Per Capita Medicaid Transfers) xtitle(Years relative the ACA expansion)
